export class Utils{
    
    /**
     * 添加点击事件的工厂  
     * @param node 触发事件的节点
     * @param target 目标节点
     * @param component 目标节点内的组件名称
     * @param handler 目标节点内的组件名称内的执行函数名
     * @param customEventData 自定义需要传递的数据
     */
    addClickEvent(node: cc.Node, target: cc.Node, component, handler, customEventData?){
        let eventHandler = new cc.Component.EventHandler();
        eventHandler.target = target;
        eventHandler.component = component;
        eventHandler.handler = handler;
        eventHandler.customEventData = customEventData;

        let clickEvents = node.getComponent(cc.Button).clickEvents;
        clickEvents.push(eventHandler);
    }


    /**
     * 添加按下ESC键的事件  
     * @param node 在这个节点下按ESC键生效
     */
    addEscEvent(node: cc.Node){

        const onKeyDown = (event: cc.Event.EventKeyboard) => {
            
        };

        const onKeyUp = (event: cc.Event.EventKeyboard) => {
            switch(event.keyCode){
                case cc.KEY.back:
                    cc.vv.alert.show('提示', '确定要退出游戏吗？', () => {
                        cc.game.end();
                    }, true);
                    break;
            }
        };

        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN.toString(), onKeyDown, node);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP.toString(), onKeyUp, node);
    }

    addSlideEvent(node: cc.Node, target: cc.Node, component, handler, customEventData?){
        let eventHandler = new cc.Component.EventHandler();
        eventHandler.target = target;
        eventHandler.component = component;
        eventHandler.handler = handler;
        eventHandler.customEventData = customEventData;

        let slideEvents = node.getComponent(cc.Slider).slideEvents;
        slideEvents.push(eventHandler);
    }
}